#include <iostream>
using std::cout;
using std::endl;

class Singleton{
public:
    static Singleton * getInstance(){
        pthread_once(&_once_control, init);
        return _pInstance;
    }
    void print(){
        cout << "Singleton::print()" << endl;
    }

private:
    static void init(){
         _pInstance = new Singleton();
    }
    friend class AutoRelease;
    Singleton(){}
    ~Singleton(){}
    static Singleton * _pInstance;
    static pthread_once_t _once_control;
};
Singleton * Singleton::_pInstance = nullptr;
pthread_once_t Singleton::_once_control = PTHREAD_ONCE_INIT;

class AutoRelease{
public:
    AutoRelease(Singleton * p)
    :_ps(p)
    {

    }

    ~AutoRelease(){
        if(_ps){
            delete _ps;
            _ps = nullptr;
        }
    }

private:
    Singleton * _ps;
};


void test(){
    //成员访问运算符进行重载  箭头和解引用
    AutoRelease ar(Singleton::getInstance());
}

int main()
{
    test();
    return 0;
}

